﻿namespace ServerApp
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Server;

    public class ProductService
    {

        #region category

        /// <summary>
        /// Add category with description, using select language
        /// </summary>
        /// <param name="categoryAll">Category with all information</param>
        /// <returns>Category with all information</returns>
        public static CategoryAll AddCategory(CategoryAll categoryAll)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                ProductCategory productCategory = new ProductCategory()
                {
                    Photo = categoryAll.Image,
                    FK_ProductCategoryId = categoryAll.ParentId
                };

                entity.AddToProductCategories(productCategory);
                entity.SaveChanges();
                if (productCategory.ProductCategoryId > 0)
                {
                    categoryAll.Id = productCategory.ProductCategoryId;
                    if (categoryAll.Localizations != null)
                        foreach (CategoryLocalizationModel item in categoryAll.Localizations)
                        {
                            ProductCategoryLocalization productCategoryLocalization = new ProductCategoryLocalization()
                            {
                                Name = item.Name,
                                Description = item.Description,
                                Language = item.Language,
                                FK_ProductCategoryId = productCategory.ProductCategoryId
                            };
                            entity.AddToProductCategoryLocalizations(productCategoryLocalization);
                        }
                }
                if (productCategory.ProductCategoryId != 0)
                {
                    entity.SaveChanges();
                    return categoryAll;
                }
                else
                {
                    return CategoryAll.Null;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Get category by identifier
        /// </summary>
        /// <param name="productParentCategoryId">Category identifier</param>
        /// <param name="currentLanguage">Current lenguage of sesion</param>
        /// <returns>List of product category with the name of the selected language</returns>
        public static CategoryView GetCategoryViewById(int productCategoryId, string currentLanguage)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                CategoryView categoryView = null;

                if (entity.ProductCategories != null && entity.ProductCategories.Count() > 0)
                {
                    foreach (ProductCategory item in entity.ProductCategories)
                    {

                        if (item.ProductCategoryId.Equals(productCategoryId))
                        {
                            if (entity.ProductCategoryLocalizations != null && entity.ProductCategoryLocalizations.Count() > 0)
                            {

                                foreach (ProductCategoryLocalization itemPl in entity.ProductCategoryLocalizations)
                                {

                                    if (itemPl.FK_ProductCategoryId.Equals(item.ProductCategoryId) && itemPl.Language.Equals(currentLanguage))
                                    {
                                        categoryView = new CategoryView(item.ProductCategoryId)
                                        {
                                            Image = item.Photo,
                                            Name = itemPl.Name,
                                            ParentId = item.FK_ProductCategoryId
                                        };
                                    }
                                }
                            }
                        }

                    }
                }

                if (categoryView != null)
                {
                    entity.SaveChanges();
                    return categoryView;
                }
                else
                {
                    return CategoryView.Null;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Get category by identifier
        /// </summary>
        /// <param name="productParentCategoryId">Category identifier</param>
        /// <param name="currentLanguage">Current lenguage of sesion</param>
        /// <returns>Category with all information</returns>
        public static CategoryAll GetCategoryAllById(int productCategoryId)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                IQueryable<ProductCategory> productCategoryList = (from p in entity.ProductCategories where p.ProductCategoryId.Equals(productCategoryId) select p);
                CategoryAll categoryAll = null;

                if (productCategoryList != null && productCategoryList.Count() > 0)
                {
                    categoryAll = new CategoryAll(productCategoryList.First().ProductCategoryId)
                    {
                        Image = productCategoryList.First().Photo,
                        ParentId = productCategoryList.First().FK_ProductCategoryId
                    };
                }
                if (entity.ProductCategoryLocalizations != null && entity.ProductCategoryLocalizations.Count() > 0)
                {
                    IQueryable<ProductCategoryLocalization> prodCategoryLocalization = (from p in entity.ProductCategoryLocalizations
                                                                                        where p.FK_ProductCategoryId.Equals(categoryAll.Id)
                                                                                        select p);
                    foreach (ProductCategoryLocalization item in prodCategoryLocalization)
                    {
                        if (categoryAll.Localizations == null)
                        {
                            categoryAll.Localizations = new List<CategoryLocalizationModel>();
                        }
                        categoryAll.Localizations.Add(new CategoryLocalizationModel()
                        {
                            Name = item.Name,
                            Language = item.Language,
                            Description = item.Description
                        });
                    }
                }

                if (categoryAll != null)
                {
                    entity.SaveChanges();
                    return categoryAll;
                }
                else
                {
                    return CategoryAll.Null;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        ///  Update category with description, using select language
        /// </summary>
        /// <param name="categoryAll">Category with all information</param>
        /// <returns>Category with all information</returns>
        public static CategoryAll UpdateCategory(CategoryAll categoryAll)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                ProductCategory productCategory = (from p in entity.ProductCategories where p.ProductCategoryId == categoryAll.Id select p).First();
                if (productCategory != null)
                {
                    productCategory.Photo = categoryAll.Image;
                    productCategory.FK_ProductCategoryId = categoryAll.ParentId;

                    List<ProductCategoryLocalization> productCategoryLocalizationList = (from p in entity.ProductCategoryLocalizations
                                                                                         where (p.FK_ProductCategoryId == categoryAll.Id)
                                                                                         select p).ToList();
                    if (productCategoryLocalizationList.Count() != 0)
                        foreach (ProductCategoryLocalization item in productCategoryLocalizationList)
                        {
                            entity.DeleteObject(item);
                        }
                    if (categoryAll.Localizations.Count() != 0)
                    {
                        foreach (CategoryLocalizationModel item in categoryAll.Localizations)
                        {
                            ProductCategoryLocalization productCategoryLocalization = new ProductCategoryLocalization()
                                {
                                    Name = item.Name,
                                    Description = item.Description,
                                    Language = item.Language,
                                    FK_ProductCategoryId = categoryAll.Id
                                };
                            entity.AddToProductCategoryLocalizations(productCategoryLocalization);
                        }
                    }
                }

                if (categoryAll != null)
                {
                    entity.SaveChanges();
                    return categoryAll;
                }
                else
                {
                    return CategoryAll.Null;
                }

            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Delete product category
        /// </summary>
        /// <param name="productCategoryId">Product category identifier</param>
        /// <returns>If operation is successful return true,elese - false</returns>
        public static bool DeleteCategory(int productCategoryId)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                if (entity.ProductCategories.Count() > 0)
                {
                    foreach (ProductCategory item in entity.ProductCategories)
                    {
                        if (item.ProductCategoryId.Equals(productCategoryId) || item.FK_ProductCategoryId.Equals(productCategoryId))
                        {
                            foreach (ProductCategoryLocalization iteml in entity.ProductCategoryLocalizations)
                            {

                                if (iteml.FK_ProductCategoryId == productCategoryId)
                                {
                                    entity.DeleteObject(iteml);
                                }
                            }
                            entity.DeleteObject(item);
                        }
                        if (entity.Products.Count() > 0)
                        {
                            foreach (Product itemp in entity.Products)
                            {

                                if (itemp.FK_ProductCategoryId == item.ProductCategoryId)
                                    DeleteProduct(itemp.ProductId);

                            }
                        }
                    }
                }
                entity.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Get category list by parent category identifier
        /// </summary>
        /// <param name="productParentCategoryId">Parent category identifier</param>
        /// <param name="currentLanguage">Current lenguage of sesion</param>
        /// <returns>List of product category with the name of the selected language</returns>
        public static List<CategoryView> GetCategories(int? productParentCategoryId, string currentLanguage)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                List<CategoryView> categoryViewList = null;

                if (entity.ProductCategories != null && entity.ProductCategories.Count() > 0)
                {
                    categoryViewList = new List<CategoryView>();
                    foreach (ProductCategory item in entity.ProductCategories)
                    {

                        if (item.FK_ProductCategoryId == productParentCategoryId)
                        {
                            if (entity.ProductCategoryLocalizations != null && entity.ProductCategoryLocalizations.Count() > 0)
                            {

                                foreach (ProductCategoryLocalization itemPl in entity.ProductCategoryLocalizations)
                                {

                                    if (itemPl.FK_ProductCategoryId.Equals(item.ProductCategoryId) && itemPl.Language.Equals(currentLanguage))
                                    {
                                        CategoryView newCategoryView = new CategoryView(item.ProductCategoryId)
                                        {
                                            Image = item.Photo,
                                            Name = itemPl.Name,
                                            ParentId = item.FK_ProductCategoryId
                                        };

                                        categoryViewList.Add(newCategoryView);
                                    }
                                }
                            }
                        }

                    }
                }
                entity.SaveChanges();
                return categoryViewList;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Get category list of Producer
        /// </summary>
        /// <param name="productParentCategoryId">Parent category identifier</param>
        /// <param name="currentLanguage">Current lenguage of sesion</param>
        /// <returns>List of product category with the name of the selected language</returns>
        public static List<CategoryView> GetCategoriesOfProducer(int? productParentCategoryId, string currentLanguage, int producerId)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                List<CategoryView> categoryViewList = null;

                //list of categories id
                List<long> subcategoriesId = new List<long>();

                //if ctegories exist
                if (entity.ProductCategories != null && entity.ProductCategories.Count() > 0)
                {

                    //for each ProductProducer get productId and then from Product get ParentCategoryId
                    foreach (ProductProducer pp in entity.ProductProducers)
                    {
                        long categId = (from prod in entity.Products where (prod.ProductId == pp.FK_ProductId && pp.FK_ProducerId == producerId) select prod.FK_ProductCategoryId).FirstOrDefault();

                        subcategoriesId.Add(categId);
                    }

                    if (subcategoriesId.Count > 0)
                    {
                        //delete duplicates 
                        subcategoriesId = subcategoriesId.Distinct().ToList();
                        subcategoriesId.Remove(0);

                        categoryViewList = new List<CategoryView>();

                        //for each id in category id list                                              
                        foreach (int categoryId in subcategoriesId)
                        {
                            //do select category
                            ProductCategory productCategory = (from pc in entity.ProductCategories where pc.ProductCategoryId == categoryId && pc.FK_ProductCategoryId == 0 select pc).FirstOrDefault();

                            //add localization
                            if (entity.ProductCategoryLocalizations != null && entity.ProductCategoryLocalizations.Count() > 0)
                            {
                                //for each localization
                                foreach (ProductCategoryLocalization itemLocal in entity.ProductCategoryLocalizations)
                                {
                                    //get localization with current language and current category
                                    if (itemLocal.FK_ProductCategoryId.Equals(categoryId) && itemLocal.Language.Equals(currentLanguage))
                                    {
                                        //Create category view
                                        CategoryView newCategoryView = new CategoryView(categoryId)
                                        {
                                            Image = productCategory.Photo,
                                            Name = itemLocal.Name, //get name from Localization table
                                            ParentId = itemLocal.FK_ProductCategoryId
                                        };

                                        //Add CategoryView to list
                                        categoryViewList.Add(newCategoryView);
                                    }

                                }//foreach ProductCategoryLocalization

                            }//if entity.ProductCategoryLocalizations != null

                        }//foreach int categoryId

                    }//if categoryViewList.Count > 0

                }// if (entity.ProductCategories != null)

                //check 
                CategoryView catView = categoryViewList[0];

                //entity.SaveChanges();
                return categoryViewList;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Get count of product pages
        /// </summary>
        /// <param name="productCategoryId">Id of the Category</param>
        /// <param name="productOnPage">number of the product</param>
        /// <returns></returns>
        public static int? GetCategoryCountPages(int productCategoryId, int productOnPage)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                double res = (from p in entity.Products where p.FK_ProductCategoryId.Equals(productCategoryId) select p).Count();
                return ((int)Math.Ceiling(res / productOnPage));
            }
            catch
            {
                return null;
            }
        }

        #endregion

        #region product

        /// <summary>
        /// Adds a range of products
        /// </summary>
        /// <param name="productAll">Product with the all information about</param>
        /// <returns>Product with the all information about</returns>
        public static ProductAll AddProduct(ProductAll productAll)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                Product product = new Product()
                {
                    Price = productAll.Price,
                    Photo = productAll.Photo,
                    FK_ProductCategoryId = productAll.CategoryId
                };
                entity.AddToProducts(product);
                entity.SaveChanges();
                if (product.ProductId > 0 && productAll.Localization != null && productAll.Localization.Count > 0)
                {
                    productAll.Id = product.ProductId;
                    if (productAll.Gallery != null && productAll.Gallery.Count() != 0)
                        foreach (string item in productAll.Gallery)
                        {
                            entity.AddToProductGalleries(new ProductGallery()
                            {
                                FK_ProductId = productAll.Id,
                                Photo = item
                            });
                        }
                    foreach (ProductLocalizationModel item in productAll.Localization)
                    {
                        entity.AddToProductLocalizations(new ProductLocalization()
                        {
                            FK_ProductId = productAll.Id,
                            Language = item.Language,
                            Name = item.Name,
                            Description = item.Description
                        });
                    }
                    productAll.Count = (from p in entity.ProductItems where p.FK_ProductId == product.ProductId select p).Count();
                }
                if (productAll.Id != 0)
                {
                    entity.SaveChanges();
                    return productAll;
                }
                else
                {
                    return ProductAll.Null;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Update a range of products
        /// </summary>
        /// <param name="productAll">Product with the all information about</param>
        /// <returns>Product with the all information about</returns>
        public static ProductAll UpdateProduct(ProductAll productAll)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                Product product = (from p in entity.Products where p.ProductId == productAll.Id select p).First();
                if (product != null)
                {
                    product.Price = productAll.Price;
                    product.Photo = productAll.Photo;
                    product.FK_ProductCategoryId = productAll.CategoryId;
                    if (productAll.Localization != null && productAll.Localization.Count > 0)
                    {
                        List<ProductLocalization> productLocalizationList = (from p in entity.ProductLocalizations where p.FK_ProductId == productAll.Id select p).ToList();

                        foreach (ProductLocalization item in productLocalizationList)
                        {
                            entity.DeleteObject(item);
                        }

                        foreach (ProductLocalizationModel item in productAll.Localization)
                        {
                            ProductLocalization productLocalization = new ProductLocalization()
                                {
                                    Name = item.Name,
                                    Description = item.Description,
                                    Language = item.Language,
                                    FK_ProductId = productAll.Id
                                };
                            entity.AddToProductLocalizations(productLocalization);
                        }
                        List<ProductGallery> productGalleryList = (from p in entity.ProductGalleries where p.FK_ProductId == productAll.Id select p).ToList();

                        foreach (ProductGallery item in productGalleryList)
                        {
                            entity.DeleteObject(item);
                        }

                        if (productAll.Gallery != null && productAll.Gallery.Count() > 0)
                            foreach (string item in productAll.Gallery)
                                entity.AddToProductGalleries(new ProductGallery() 
                                { 
                                    FK_ProductId = productAll.Id, 
                                    Photo = item 
                                });

                        productAll.Count = (from p in entity.ProductItems where p.FK_ProductId == productAll.Id select p).Count();
                    }
                }

                if (productAll != null)
                {
                    entity.SaveChanges();
                    return productAll;
                }
                else
                {
                    return ProductAll.Null;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Adds a range of products
        /// </summary>
        /// <param name="productId">Identificator of product</param>
        /// <param name="price">Price of product</param>
        /// <param name="photo">Product photo</param>
        /// <param name="productCategoryId">Product category identifier</param>
        /// <param name="gallery">List of url product photos</param>
        /// <param name="productLocalizationModelList">List of name and description of product on selected language</param>
        /// <param name="currentLanguage">Current lenguage of session</param>
        /// <returns>Product with the name of the selected language</returns>
        public static ProductAll GetProductAllById(long productAllId)
        {
            try
            {
                ProductAll productAll = null;
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                Product product = (from p in entity.Products where p.ProductId == productAllId select p).First();
                if (product != null)
                {
                    productAll = new ProductAll(product.ProductId);
                    productAll.Price = product.Price;
                    productAll.Photo = product.Photo;
                    productAll.CategoryId = product.FK_ProductCategoryId;

                    IQueryable<ProductLocalization> productLocalizationList = (from p in entity.ProductLocalizations where p.FK_ProductId == productAll.Id select p);
                    foreach (ProductLocalization item in productLocalizationList)
                    {
                        if (productAll.Localization == null)
                        {
                            productAll.Localization = new List<ProductLocalizationModel>();
                        }

                        productAll.Localization.Add(new ProductLocalizationModel()
                            {
                                Name = item.Name,
                                Language = item.Language,
                                Description = item.Description
                            });
                    }
                    IQueryable<ProductGallery> productGalleryList = (from p in entity.ProductGalleries where p.FK_ProductId == productAll.Id select p);
                    foreach (ProductGallery item in productGalleryList)
                    {
                        if (productAll.Gallery == null)
                        {
                            productAll.Gallery = new List<string>();
                        }

                        productAll.Gallery.Add(item.Photo);
                    }
                    productAll.Count = (from p in entity.ProductItems where p.FK_ProductId == productAll.Id select p).Count();
                }
                if (productAll != null)
                {
                    entity.SaveChanges();
                    return productAll;
                }
                else
                {
                    return ProductAll.Null;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Delete product 
        /// </summary>
        /// <param name="productCategoryId">Product  identifier</param>
        /// <returns>If operation is successful return true,elese - false</returns>
        public static bool DeleteProduct(long productId)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                foreach (var item in entity.ProductLocalizations)
                {

                    if (item.FK_ProductId.Equals(productId))
                    {
                        entity.DeleteObject(item);
                    }

                }
                foreach (var item in entity.ProductGalleries)
                {

                    if (item.FK_ProductId.Equals(productId))
                    {
                        entity.DeleteObject(item);
                    }

                }

                foreach (var item in entity.ProductItems)
                {

                    if (item.FK_ProductId.Equals(productId))
                    {
                        entity.DeleteObject(item);
                    }

                }
                foreach (var item in entity.Products)
                {

                    if (item.ProductId.Equals(productId))
                    {
                        entity.DeleteObject(item);
                    }

                }
                entity.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Get product list by category identifier
        /// </summary>
        /// <param name="productCategoryId">Category identifier</param>
        /// <param name="currentLanguage">Current lenguage of sesion</param>
        /// <returns>List of product with the name of the selected language</returns>
        public static List<ProductView> GetProducts(int productCategoryId, string currentLanguage)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                List<ProductView> productViewList = null;

                if (entity.Products != null && entity.Products.Count() > 0)
                {
                    productViewList = new List<ProductView>();
                    foreach (Product item in entity.Products)
                    {

                        if (item.FK_ProductCategoryId == productCategoryId)
                        {
                            if (entity.ProductLocalizations != null && entity.ProductLocalizations.Count() > 0)
                            {
                                ProductLocalization prodLocal = (from p in entity.ProductLocalizations
                                                                 where p.FK_ProductId.Equals(item.ProductId) && p.Language.Equals(currentLanguage)
                                                                 select p).ToList().First();
                                if (prodLocal != null)
                                {
                                    ProductView newProductView = new ProductView(item.ProductId)
                                    {
                                        CategoryId = productCategoryId,
                                        Count = (from p in entity.ProductItems where p.FK_ProductId == item.ProductId select p).Count(),
                                        Price = item.Price,
                                        Photo = item.Photo,
                                        Name = prodLocal.Name,
                                    };

                                    productViewList.Add(newProductView);
                                }

                            }
                        }

                    }
                }
                entity.SaveChanges();
                return productViewList;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Get sorted product list by category identifier 
        /// </summary>
        /// <param name="productCategoryId">Category identifier</param>
        /// <param name="currentLanguage">Current lenguage of sesion</param>
        /// <param name="productSort">Sort parametr</param>
        /// <returns>Sort list of product with the name of the selected language</returns>
        public static List<ProductView> GetSortProducts(int productCategoryId, string currentLanguage, ProductSort productSort)
        {
            try
            {
                List<ProductView> productViewList = new List<ProductView>();
                productViewList = GetProducts(productCategoryId, currentLanguage);
                switch (productSort)
                {
                    case ProductSort.PriceAsc:
                        productViewList = (from p in productViewList orderby p.Price ascending select p).ToList();
                        break;
                    case ProductSort.PriceDesc:
                        productViewList = (from p in productViewList orderby p.Price descending select p).ToList();
                        break;
                    case ProductSort.NameAsc:
                        productViewList = (from p in productViewList orderby p.Name ascending select p).ToList();
                        break;
                    case ProductSort.NameDesc:
                        productViewList = (from p in productViewList orderby p.Name descending select p).ToList();
                        break;
                    default:
                        productViewList = (from p in productViewList orderby p.Price ascending select p).ToList();
                        break;
                }
                return productViewList;
            }
            catch
            {
                return null;
            }
        }

        public static List<ProductView> GetAllProductsOfProducer(int producerId, string currentLanguage)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                List<ProductView> productViewList = null;

                if (entity.Products != null && entity.Products.Count() > 0)
                {
                    productViewList = new List<ProductView>();

                    //get list of ProductProducers. It has a FK to Product

                    List<ProductProducer> productProducers = (from pp in entity.ProductProducers where pp.FK_ProducerId == producerId select pp).ToList();

                    //for each ProductProducer, get 
                    foreach (ProductProducer pp in productProducers)
                    {
                        if (entity.ProductLocalizations != null && entity.ProductLocalizations.Count() > 0)
                        {
                            //get a localization for the product
                            ProductLocalization prodLocal = (from p in entity.ProductLocalizations
                                                             where p.FK_ProductId.Equals(pp.FK_ProductId) && p.Language.Equals(currentLanguage)
                                                             select p).ToList().First();
                            if (prodLocal != null)
                            {
                                ProductView newProductView = new ProductView(pp.FK_ProductId)
                                {
                                    CategoryId = 0,  //0 because we need not know the category of the product
                                    Count = 1, //we don't need to know the count of products
                                    Price = pp.Product.Price,
                                    Photo = pp.Product.Photo,
                                    Name = prodLocal.Name,
                                };

                                productViewList.Add(newProductView);
                            }

                        }//if entity.ProductLocalizations
                    }//foreach (ProductProducer pp in productProducers)

                }//if entity.Products != null

                return productViewList;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Get sorted product list by category identifier on selected page
        /// </summary>
        /// <param name="productCategoryId">Category identifier</param>
        /// <param name="currentLanguage">Current lenguage of sesion</param>
        /// <param name="productSort">Sort parametr</param>
        /// <param name="page">Page number</param>
        /// <param name="countOnPage">Count products on page</param>
        /// <returns></returns>
        //public static List<ProductView> GetProductsOfProducerOnPage(string currentLanguage, ProductSort productSort, int page, int countOnPage, Dictionary<ProductFilter, object> filters)
        //{
        //    try
        //    {
        //        List<ProductView> productViewList = new List<ProductView>();
        //        int fromNumb = ((page - 1) * countOnPage);
        //        productViewList = GetProducts(productCategoryId, currentLanguage);
        //        if (filters != null)
        //        {
        //            foreach (ProductFilter key in filters.Keys)
        //            {
        //                switch (key)
        //                {
        //                    case ProductFilter.PriceMax:
        //                        {
        //                            decimal price = Convert.ToDecimal(filters[key]);
        //                            productViewList = (from p in productViewList where p.Price <= price select p).ToList();
        //                            break;
        //                        }
        //                    case ProductFilter.PriceMin:
        //                        {
        //                            decimal price = Convert.ToDecimal(filters[key]);
        //                            productViewList = (from p in productViewList where p.Price >= price select p).ToList();
        //                            break;
        //                        }
        //                    case ProductFilter.Non: break;
        //                }
        //            }
        //        }

        //        switch (productSort)
        //        {
        //            case ProductSort.PriceAsc:
        //                productViewList = (from p in productViewList orderby p.Price ascending select p).Skip(fromNumb).Take(countOnPage).ToList();
        //                break;
        //            case ProductSort.PriceDesc:
        //                productViewList = (from p in productViewList orderby p.Price descending select p).Skip(fromNumb).Take(countOnPage).ToList();
        //                break;
        //            case ProductSort.NameAsc:
        //                productViewList = (from p in productViewList orderby p.Name ascending select p).Skip(fromNumb).Take(countOnPage).ToList();
        //                break;
        //            case ProductSort.NameDesc:
        //                productViewList = (from p in productViewList orderby p.Name descending select p).Skip(fromNumb).Take(countOnPage).ToList();
        //                break;
        //            default:
        //                productViewList = (from p in productViewList orderby p.Price ascending select p).Skip(fromNumb).Take(countOnPage).ToList();
        //                break;
        //        }
        //        return productViewList;
        //    }
        //    catch
        //    {
        //        return null;
        //    }
        //}

        /// <summary>
        /// Get sorted product list by category identifier on selected page
        /// </summary>
        /// <param name="productCategoryId">Category identifier</param>
        /// <param name="currentLanguage">Current lenguage of sesion</param>
        /// <param name="productSort">Sort parametr</param>
        /// <param name="page">Page number</param>
        /// <param name="countOnPage">Count products on page</param>
        /// <returns>List<ProductView></returns>
        public static List<ProductView> GetProductsOnPage(int productCategoryId, string currentLanguage, ProductSort productSort, int page, int countOnPage, Dictionary<ProductFilter, object> filters)
        {
            try
            {
                List<ProductView> productViewList = new List<ProductView>();
                int fromNumb = ((page - 1) * countOnPage);
                productViewList = GetProducts(productCategoryId, currentLanguage);
                if (filters != null)
                {
                    foreach (ProductFilter key in filters.Keys)
                    {
                        switch (key)
                        {
                            case ProductFilter.PriceMax:
                                {
                                    decimal price = Convert.ToDecimal(filters[key]);
                                    productViewList = (from p in productViewList where p.Price <= price select p).ToList();
                                    break;
                                }
                            case ProductFilter.PriceMin:
                                {
                                    decimal price = Convert.ToDecimal(filters[key]);
                                    productViewList = (from p in productViewList where p.Price >= price select p).ToList();
                                    break;
                                }
                            case ProductFilter.Non: break;
                        }
                    }
                }

                switch (productSort)
                {
                    case ProductSort.PriceAsc:
                        productViewList = (from p in productViewList orderby p.Price ascending select p).Skip(fromNumb).Take(countOnPage).ToList();
                        break;
                    case ProductSort.PriceDesc:
                        productViewList = (from p in productViewList orderby p.Price descending select p).Skip(fromNumb).Take(countOnPage).ToList();
                        break;
                    case ProductSort.NameAsc:
                        productViewList = (from p in productViewList orderby p.Name ascending select p).Skip(fromNumb).Take(countOnPage).ToList();
                        break;
                    case ProductSort.NameDesc:
                        productViewList = (from p in productViewList orderby p.Name descending select p).Skip(fromNumb).Take(countOnPage).ToList();
                        break;
                    default:
                        productViewList = (from p in productViewList orderby p.Price ascending select p).Skip(fromNumb).Take(countOnPage).ToList();
                        break;
                }
                return productViewList;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Get full product description by it identifier
        /// </summary>
        /// <param name="productId">Product identifierr</param>
        /// <param name="currentLanguage">>Current lenguage of sesion</param>
        /// <returns>Full information of product on selected language</returns>
        public static ProductFull GetProductById(long productId, string currentLanguage)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                Product product = (from p in entity.Products where p.ProductId == productId select p).First();
                ProductFull productFull = null;
                if (product != null)
                {

                    if (entity.ProductLocalizations != null && entity.ProductLocalizations.Count() > 0)
                    {
                        ProductLocalization prodLocal = (from p in entity.ProductLocalizations
                                                         where p.FK_ProductId.Equals(product.ProductId) && p.Language.Equals(currentLanguage)
                                                         select p).ToList().First();

                        productFull = new ProductFull(productId)
                       {
                           Photo = product.Photo,
                           Price = product.Price,
                           Count = (from p in entity.ProductItems where p.FK_ProductId == product.ProductId select p).Count(),
                           CategoryId = product.FK_ProductCategoryId,
                           Name = prodLocal.Name,
                           Description = prodLocal.Description,
                           Gallery = new List<string>()
                       };

                    }
                    if (entity.ProductGalleries != null && entity.ProductGalleries.Count() > 0)
                    {
                        productFull.Gallery = (from p in entity.ProductGalleries where p.FK_ProductId.Equals(product.ProductId) select p.Photo).ToList();
                    }
                }
                if (productFull != null)
                {
                    entity.SaveChanges();
                    return productFull;
                }
                else
                {
                    return ProductFull.Null;
                }
            }
            catch
            {
                return null;
            }
        }

        #endregion

        #region product item

        /// <summary>
        /// Update product item in base
        /// </summary>
        /// <param name="productItemModel">Changed product item</param>
        /// <returns>Product item</returns>
        public static ProductItemModel GetProductItemById(long productItemId)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                IQueryable<ProductItem> prodList = (from p in entity.ProductItems where p.ProductItemId == productItemId select p);
                ProductItemModel productItemModel = null;
                if (prodList != null && prodList.Count() > 0)
                {
                    ProductItem productItem = prodList.First();
                    productItemModel = new ProductItemModel(productItem.ProductItemId);
                    productItemModel.Serial = productItem.Serial;
                    productItemModel.Status = (ProductItemStatus)productItem.Status;
                    productItemModel.OrderId = productItem.FK_OrderID;
                    productItemModel.PlaceId = productItem.FK_PlaceId;
                    productItemModel.ProductId = productItem.FK_ProductId;
                    return productItemModel;
                }
                else
                {
                    return ProductItemModel.Null;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Add product item in base
        /// </summary>
        /// <param name="productItemModel">Changed product item</param>
        /// <returns>Product item</returns>
        public static ProductItemModel AddProductItem(ProductItemModel productItemModel)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                ProductItem productItem = new ProductItem()
                {
                    Serial = productItemModel.Serial,
                    Status = Convert.ToByte(productItemModel.Status),
                    FK_PlaceId = productItemModel.PlaceId,
                    FK_ProductId = productItemModel.ProductId,
                    FK_OrderID = productItemModel.OrderId
                };
                entity.AddToProductItems(productItem);
                entity.SaveChanges();
                if (productItem.ProductItemId > 0)
                {
                    productItemModel.Id = productItem.ProductItemId;
                    return productItemModel;
                }
                else
                {
                    return ProductItemModel.Null;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Update product item in base
        /// </summary>
        /// <param name="productItemModel">Changed product item</param>
        /// <returns>Product item</returns>
        public static ProductItemModel UpdateProductItem(ProductItemModel productItemModel)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                IQueryable<ProductItem> prodList = (from p in entity.ProductItems where p.ProductItemId == productItemModel.Id select p);
                if (prodList != null && prodList.Count() > 0)
                {
                    ProductItem productItem = prodList.First();
                    productItem.Serial = productItemModel.Serial;
                    productItem.Status = Convert.ToByte(productItemModel.Status);
                    productItem.FK_PlaceId = productItemModel.PlaceId;
                    productItem.FK_ProductId = productItemModel.ProductId;
                    productItem.FK_OrderID = productItemModel.OrderId;

                    productItemModel.Serial = productItem.Serial;
                    productItemModel.Status = (ProductItemStatus)productItem.Status;
                    productItemModel.OrderId = productItem.FK_OrderID;
                    productItemModel.PlaceId = productItem.FK_PlaceId;
                    productItemModel.ProductId = productItem.FK_ProductId;
                    entity.SaveChanges();
                    return productItemModel;
                }
                else
                {
                    return ProductItemModel.Null;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Delete product item from base
        /// </summary>
        /// <param name="productItemId">Product item identifier</param>
        /// <returns>If operation is successful return true,elese - false</returns>
        public static bool DeleteProductItem(long productItemId)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                IQueryable<ProductItem> prList = (from p in entity.ProductItems where p.ProductItemId == productItemId select p);
                if (prList != null && prList.Count() > 0)
                {
                    ProductItem productItem = prList.First();
                    entity.DeleteObject(productItem);
                    entity.SaveChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        #endregion
    }
}
